package com.dt.global.web.action.Newscentrality;

import java.awt.Image;
import java.io.File;
import java.util.List;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;

import com.dt.components.exstruts.action.BaseActionSupport;
import com.dt.global.entitys.bo.TWebCoreArticle;
import com.dt.global.utils.Proerties;
import com.dt.global.web.mar.Newscentrality.NewscentralityMar;

/**
 * 初始化首页的方法
 * @author MWG
 *
 */
@SuppressWarnings("serial")
@Scope("prototype")
public class InitNewscentralityAction extends BaseActionSupport{
	@Resource
	public NewscentralityMar newMar;
	public String initIndex() {
		try{
			//默认显示五张
			int rows = 5;
			String path = getRequest().getContextPath();
			Proerties popUtil = new Proerties();
			String url = popUtil.getValue("wjrdht");
			String pathDetail = path + "/article/getNewsInfoClose.do";
			String spics = null;
			String stexts = null;
			String slinks = null;
			//加载新闻资讯图片新闻
			List<TWebCoreArticle> listxwzx=newMar.getTWebCoreArticle("channelId201309251495122329289728");
			if (listxwzx!=null&&listxwzx.size()!=0){
				if(listxwzx.size()<5){
					rows = listxwzx.size();
				}
				for (int getrows=0;getrows<rows;getrows++){
					TWebCoreArticle entityA = (TWebCoreArticle) listxwzx.get(getrows);
					 String path1 =ServletActionContext.getServletContext().getRealPath(entityA.getTitleImg());
					   File file = new File(path1);
//					   System.out.println(url+"/uploadfile"+entityA.getTitleImg());
////					   file.
//					 Image srcFile = ImageIO.read(new File("http:\\localhost:9080/wjrdht/uploadfile/3a8ae7f2-acfc-4434-ac66-17441ebcdbc8.jpg"));
//					 
//					 int w = srcFile.getWidth(null);
//				     int h = srcFile.getHeight(null);
//					System.out.println(w+h);
					if(spics==null){
						spics = url+"/uploadfile"+entityA.getTitleImg();
					}else{
						spics =spics+"|"+url+"/uploadfile"+entityA.getTitleImg();
					}
					if(stexts==null){
						stexts = entityA.getTitle();
						stexts = stexts.length()>15?stexts.substring(0, 15)+"..":stexts;
					}else{
						String title = entityA.getTitle().length()>15?entityA.getTitle().substring(0, 15)+"..":entityA.getTitle();
						stexts = stexts+"|"+ title;
					}
					if(slinks==null){
						slinks = pathDetail + "?articleId="+entityA.getArticleId();
					}else{
						slinks = slinks + "|" + pathDetail + "?articleId="+entityA.getArticleId();
					}
				}
			}
			getRequest().setAttribute("spics", spics);
			getRequest().setAttribute("stexts", stexts);
			getRequest().setAttribute("slinks", slinks);
			return "success";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
}
